package org.dizem.intellipainter.model;

import org.dizem.intellipainter.MainFrame;

import java.awt.*;

/**
 * User: dizem@126.com
 * Time: 12-2-18 下午8:55
 */
public class Shape {
	private ShapeType shapeType;
	private final Point startPoint;
	private Point endPoint;
	private final Color color1;
	private Color color2;
	private StrokeType strokeType;
	private final boolean isText;
	private String text;

	public Shape(ShapeType shapeType, StrokeType strokeType, Point startPoint, Point endPoint, Color color1, Color color2) {
		this.shapeType = shapeType;
		this.strokeType = strokeType;
		this.startPoint = startPoint;
		this.endPoint = endPoint;
		this.color1 = color1;
		this.color2 = color2;
		isText = false;
	}

	public Shape(String text, Color color1, Point startPoint) {
		this.text = text;
		this.startPoint = startPoint;
		this.color1 = color1;
		isText = true;
	}

	public Point getEndPoint() {
		return endPoint;
	}

	public Point getStartPoint() {
		return startPoint;
	}

	public ShapeType getShapeType() {
		return shapeType;
	}

	public Color getColor1() {
		return color1;
	}

	public Color getColor2() {
		return color2;
	}

	public void draw(Graphics g) {
		g.setColor(color1);
		if (isText) {
			g.setFont(MainFrame.FONT);
			g.drawString(text, startPoint.x, startPoint.y);
			return;
		}
		((Graphics2D) g).setStroke(strokeType.getStroke());
		switch (shapeType) {
			case Point: {
				if (strokeType != null && endPoint != null) {
					int width = (int) strokeType.getStroke().getLineWidth();
					g.fillOval(endPoint.x - width / 2, endPoint.y - width / 2, width, width);
				}
				break;
			}
			case Rect: {
				int leftTopX = Math.min(startPoint.x, endPoint.x);
				int leftTopY = Math.min(startPoint.y, endPoint.y);
				int width = Math.abs(endPoint.x - startPoint.x);
				int height = Math.abs(endPoint.y - startPoint.y);
				g.drawRect(leftTopX, leftTopY, width, height);
				break;
			}
			case Circle: {
				int leftTopX = Math.min(startPoint.x, endPoint.x);
				int leftTopY = Math.min(startPoint.y, endPoint.y);
				int width = Math.abs(endPoint.x - startPoint.x);
				int height = Math.abs(endPoint.y - startPoint.y);
				g.drawOval(leftTopX, leftTopY, width, height);
				break;
			}
			case Line:
				g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
				break;
			case FillRect: {
				int leftTopX = Math.min(startPoint.x, endPoint.x);
				int leftTopY = Math.min(startPoint.y, endPoint.y);
				int width = Math.abs(endPoint.x - startPoint.x);
				int height = Math.abs(endPoint.y - startPoint.y);
				g.setColor(color2);
				g.fillRect(leftTopX + 1, leftTopY + 1, width - 1, height - 1);
				g.setColor(color1);
				g.drawRect(leftTopX, leftTopY, width, height);
				break;
			}
			case FillCircle: {
				int leftTopX = Math.min(startPoint.x, endPoint.x);
				int leftTopY = Math.min(startPoint.y, endPoint.y);
				int width = Math.abs(endPoint.x - startPoint.x);
				int height = Math.abs(endPoint.y - startPoint.y);
				g.setColor(color2);
				g.fillOval(leftTopX + 1, leftTopY + 1, width - 1, height - 1);
				g.setColor(color1);
				g.drawOval(leftTopX, leftTopY, width, height);
				break;
			}
		}
	}
}
